package se480.tracker.proxy;

import org.apache.log4j.Logger;

import se480.tracker.event.TrackerEvent;
import se480.tracker.service.RegistrationService;
import se480.tracker.tracker.Tracker;
import se480.tracker.tracker.TrackerObserver;

/**
 * This is an asynchronous proxy.  It abstracts the update client 
 * from the TrackerA oject.  Several clients could use a single
 * proxy.  This could help scaling.
 * 
 * Also; the proxy abstracts primary and backup Trackers from the clients, 
 * as well as the failover process.
 * 
 * @author mmichalak
 *
 */
public class AsynchProxy implements TrackerProxy, TrackerObserver {
	static Logger logger = Logger.getLogger(AsynchProxy.class.getName());
	
	RegistrationService regSvc = null;
	Tracker aTracker = null;

	public String processRequest(TrackerEvent ev) {
		logger.debug("Beginning process");
		
		if (aTracker == null) {
			aTracker = regSvc.getTrackerA(this);
		}
		
		AsynchCommunication asynchComm = new AsynchCommunication();
		asynchComm.setAsynchEvent(ev);
		asynchComm.setTrackerA(aTracker);
		Thread thr = new Thread(asynchComm);
        thr.setDaemon(true);
        thr.start();
        
        return null;
	}

	public RegistrationService getRegSvc() {
		return regSvc;
	}

	public void setRegSvc(RegistrationService regSvc) {
		this.regSvc = regSvc;
	}

	public Tracker getaTracker() {
		return aTracker;
	}

	public void setaTracker(Tracker aTracker) {
		this.aTracker = aTracker;
	}

	/**
	 * Tracker Failover observer callback method
	 */
	public void updateTracker(Tracker tracker) {
		aTracker = tracker;	
	}
}
